/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package entreprise;
import entreprise.Drug;
import java.sql.SQLException;
import java.util.ArrayList;
import mapping.DBDrug;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Quoc-Khai
 */
public class DrugService {
    public Drug getDrug(String drugID){
        DBDrug bDrug=new DBDrug();
        Drug drug=null;        
        try {
            drug=bDrug.getDrug(drugID);
        } catch (SQLException ex) {
            Logger.getLogger(DrugService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return drug;
    }
    public boolean createDrug(String drugID,String drugName, float drugPrice, String drugDescripton){
        //check drug ton tai hay chua, goi ham getDrug(drugID) trong mapping
        //neu chua ton tai thi tao doi tuong drug, roi su dung ham createDrug(drug) trong mapping de luu
        DBDrug dbDrug = new DBDrug();
        Drug iDrug=new Drug();
        iDrug=new Drug();
        iDrug.setDrugName(drugName);
        iDrug.setDrugPrice(drugPrice);
        iDrug.setDrugDescription(drugDescripton);
        try{
            dbDrug.createDrug(iDrug);
            return true;
        }catch(SQLException e){       
            return false;
        }
    }
    public boolean deleteDrug(String drugID){
        //check drug ton tai hay chua-> neu chua thi dua ra thong bao
        //goi ham deleteDrug tai lop mapping de xoa drug trong database
        DBDrug dbDrug = new DBDrug();
        Drug iDrug = new Drug();
        try {
            iDrug = dbDrug.getDrug(drugID);
        } catch (SQLException ex) {
            Logger.getLogger(DrugService.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (iDrug!=null) {
            dbDrug.deleteDrug(drugID);
            return true;
        }
        
//        System.out.println("Drug not existe!");
        return false;
    }
    public boolean updateDrug(String drugID,String drugName, float drugPrice, String drugDescripton){
        //check drug ton tai hay chua bang ham getDrug() trong mapping
        //neu tim thay, update thong tin cho drug nay, dung cac ham get, set trong lop drug
        //goi ham updateDrug() trong mapping de luu
        DBDrug dbDrug = new DBDrug();
        Drug iDrug = new Drug();
        try {
            iDrug = dbDrug.getDrug(drugID);
        } catch (SQLException ex) {
            Logger.getLogger(DrugService.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (iDrug!=null) {
            iDrug.setDrugID(drugID);
            iDrug.setDrugName(drugName);
            iDrug.setDrugPrice(drugPrice);
            iDrug.setDrugDescription(drugDescripton);
            if(dbDrug.updateDrug(iDrug))return true;
            else return false;
        }
        return false;
    }
    
    public int getNumberOfDrugs(String drugName){
        DBDrug dbDrug=new DBDrug();
        return dbDrug.getNumberOfDrugs(drugName);
    }    
    public ArrayList<Drug> drugs(String drugName, int from, int numberOfRow){
        DBDrug dbDrug=new DBDrug();
        ArrayList<Drug> drugs=dbDrug.getDrugs(drugName,from,numberOfRow);
        return drugs;
    }
}
